package com.moli.iov.handle.alarm.biz.real.extend.platform;

import com.moli.iov.base.enums.AlarmAuthEnum;
import com.moli.iov.base.model.dto.SysParamSettingsCacheBean;
import com.moli.iov.base.remote.ISysParamSettingsRemoteService;
import com.moli.iov.handle.alarm.constants.Constant;
import com.moli.iov.handle.alarm.enums.AlarmCodeEnum;
import com.moli.iov.handle.alarm.enums.AlarmSourceEnum;
import com.moli.iov.handle.alarm.enums.AlarmTypeEnum;
import com.moli.iov.handle.alarm.enums.AntiShakeEnum;
import com.moli.iov.handle.alarm.model.dto.AlarmDto;
import com.moli.iov.handle.alarm.model.dto.AuthDto;
import com.moli.iov.handle.alarm.model.dto.DisarmAlarmDto;
import com.moli.iov.handle.alarm.service.IAlarmInfoRequestService;
import com.moli.iov.protocol.model.realtime.ExtendReq;
import com.moli.iov.protocol.model.realtime.RealTimeInfoReq;
import com.moli.iov.rest.RestResponse;
import java.util.Arrays;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * <p>
 *  蓄电池低电压报警  低于11V
 * </p>
 *
 * @author zql
 * @since 2019/10/11 15:36
 */
@Slf4j
@Component
public class BatteryVoltageReal {

  @Autowired
  private IAlarmInfoRequestService alarmInfoRequestService;

  @Autowired
  private ISysParamSettingsRemoteService sysParamSettingsRemoteService;


  /**
   * 蓄电池低电压报警  低于11V
   * @param vin
   * @param realTimeInfoReq
   */
  public void handle(String vin, RealTimeInfoReq realTimeInfoReq, AuthDto authDto){
    try{
      batteryVoltage(vin, realTimeInfoReq, authDto);
    }catch (Exception e){
      log.error("蓄电池低电压报警 异常", e);
    }
  }

  /**
   * 蓄电池低电压报警  低于11V
   * @param vin
   * @param realTimeInfoReq
   */
  public void batteryVoltage(String vin, RealTimeInfoReq realTimeInfoReq, AuthDto authDto){
    try{
      ExtendReq extendReq = realTimeInfoReq.extendReq;

      if(extendReq == null ||
          extendReq.extendVehicleState == null) {
        log.error("蓄电池低电压报警  低于11V 失败，extendReq 或 " +
            "extendVehicleState 为null vin:{}", vin);
        return;
      }

      double batteryVoltage =
          extendReq.extendVehicleState.getExtendBatteryVoltage();

      if(batteryVoltage <= Constant.ERROR_VOLTAGE){
        return;
      }

      if(batteryVoltage < getBatteryVoltageThreshold(vin)){

        if(authDto.getAlarmAuths().contains(AlarmAuthEnum.REAL.getFlag())){
          log.info("蓄电池低电压报警 不监控报警 vin:{}", vin);
          return;
        }

        log.info("蓄电池低电压报警 vin:{} 电压：{}",
            vin, batteryVoltage);

        double longitude = realTimeInfoReq.vehicleGpsReq != null ? realTimeInfoReq.vehicleGpsReq.longitude : 0;
        double latitude = realTimeInfoReq.vehicleGpsReq != null ? realTimeInfoReq.vehicleGpsReq.latitude : 0;

        alarmInfoRequestService.alarm(new AlarmDto(
            vin,
            AlarmCodeEnum.BATTERY_VOLTAGE,
            AlarmTypeEnum.ALARM.getFlag(),
            AlarmSourceEnum.PLATFORM.getFlag(),
            "",
            AntiShakeEnum.YES.getFlag(),
            longitude,
            latitude));

        return;
      }

      alarmInfoRequestService.disarmAlarm(new DisarmAlarmDto(vin,
          AlarmCodeEnum.BATTERY_VOLTAGE.getFlag(),
          AntiShakeEnum.YES.getFlag()));
    }catch(Exception e){
      log.error("蓄电池低电压报警  低于11V 添加线程池异常", e);
    }
  }

  /**
   * 获取 BatteryVoltage 阈值
   * @param vin
   * @return
   */
  private double getBatteryVoltageThreshold(String vin) {

    try{
      RestResponse<Map<String, SysParamSettingsCacheBean>> restResponse =
          sysParamSettingsRemoteService.getSysParamSettingsCacheBeanByVin(Arrays.asList(vin));

      if(restResponse == null || !restResponse.flag || restResponse.getData() == null){
        return Constant.ALARM_BATTERY_VOLTAGE;
      }

      Map<String, SysParamSettingsCacheBean> map = restResponse.getData();

      SysParamSettingsCacheBean sysParamSettingsCacheBean = map.get(vin);

      if(sysParamSettingsCacheBean == null || sysParamSettingsCacheBean.getLowVoltage() < 0){
        return Constant.ALARM_BATTERY_VOLTAGE;
      }

      return sysParamSettingsCacheBean.getLowVoltage();
    }catch(Exception e){
      log.error("获取 BatteryVoltage 阈值 异常，调用Base服务失败 vin:{}, 错误信息：{}", vin, e.getMessage());
      return Constant.ALARM_BATTERY_VOLTAGE;
    }
  }
}
